
package com.yanboo.modules.system.rest;

import com.yanboo.common.annotation.CustomController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import com.yanboo.logging.annotation.Log;
import com.yanboo.common.exception.BadRequestException;
import com.yanboo.modules.system.domain.DictDetail;
import com.yanboo.modules.system.service.DictDetailService;
import com.yanboo.modules.system.service.dto.DictDetailDto;
import com.yanboo.modules.system.service.dto.DictDetailQueryCriteria;
import com.yanboo.common.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统：字典详情管理
 */
@Tag(name = "系统:字典详情管理")
@CustomController
@RequiredArgsConstructor
@RequestMapping("/api/dictDetail")
public class DictDetailController {

    private final DictDetailService dictDetailService;
    private static final String ENTITY_NAME = "dictDetail";

    /**
     * 查询字典详情
     * @param criteria
     * @param pageable
     * @return
     */
    @Operation(summary = "查询字典详情")
    @GetMapping
    public ResponseEntity<PageResult<DictDetailDto>> queryDictDetail(DictDetailQueryCriteria criteria,
                                                                     @PageableDefault(sort = {"dictSort"}, direction = Sort.Direction.ASC) Pageable pageable){
        return new ResponseEntity<>(dictDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
     * 查询多个字典详情
     * @param dictName
     * @return
     */
    @Operation(summary = "查询多个字典详情")
    @GetMapping(value = "/map")
    public ResponseEntity<Object> getDictDetailMaps(@RequestParam String dictName){
        String[] names = dictName.split("[,，]");
        Map<String, List<DictDetailDto>> dictMap = new HashMap<>(16);
        for (String name : names) {
            dictMap.put(name, dictDetailService.getDictByName(name));
        }
        return new ResponseEntity<>(dictMap, HttpStatus.OK);
    }

    /**
     * 新增字典详情
     * @param resources
     * @return
     */
    @Operation(summary = "新增字典详情")
    @Log("新增字典详情")
    @PostMapping
    public ResponseEntity<Object> createDictDetail(@Validated @RequestBody DictDetail resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        dictDetailService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改字典详情
     * @param resources
     * @return
     */
    @Operation(summary = "修改字典详情")
    @Log("修改字典详情")
    @PutMapping
    public ResponseEntity<Object> updateDictDetail(@Validated(DictDetail.Update.class) @RequestBody DictDetail resources){
        dictDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除字典详情
     * @param id
     * @return
     */
    @Operation(summary = "删除字典详情")
    @Log("删除字典详情")
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Object> deleteDictDetail(@PathVariable Long id){
        dictDetailService.delete(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}